<?php

namespace App\Admin\Controllers;

use App\Models\DealRecord;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class DealRecordController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('交易记录');
            $content->description('充值，提现');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(DealRecord::class, function (Grid $grid) {

            $grid->disableCreation();
            $grid->actions(function ($actions) {
                $actions->disableEdit();
            });

            $grid->id('ID')->sortable();
            $grid->user_id('用户id');
            $grid->amount('金额');
            $grid->status('交易状态')->display(function ($data){
                switch ($data){
                    case 0: return '挂起';
                    case 1: return '成功';
                    case 2: return '失败';
                    default:return '异常';
                }
            });
            $grid->description('交易描述');
            $grid->in_order_num('本交易单号');
            $grid->out_order_num('外交易单号');
            $grid->pay_type('支付方式')->display(function ($data){
               switch ($data){
                   case 1:return '支付宝';
                   case 2:return '微信';
                   default:return '未知';
               }
            });

            $grid->stime('交易开始时间')->display(function ($data){
                return date('Y-m-d H:i:s',$data);
            });
            $grid->etime('交易关闭时间')->display(function ($data){
                return date('Y-m-d H:i:s',$data);
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(DealRecord::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
